package com.perye.dokit.service.impl;

import cn.hutool.core.util.IdUtil;
import com.perye.dokit.entity.WeWeddingStyle;
import com.perye.dokit.mapper.dao.DaoSupport;
import com.perye.dokit.service.DictDetailService;
import com.perye.dokit.service.PersonnelService;
import com.perye.dokit.service.WeWeddingStyleService;
import com.perye.dokit.utils.DateUtils;
import com.perye.dokit.utils.StringUtils;
import com.perye.dokit.utils.UuidUtil;
import com.perye.dokit.utils.pageData.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PersonnelServiceImpl
 * @Description 人员管理接口实现类
 * @Date 2022/3/17 14:15
 * @Version 1.0
 **/
@Service
public class PersonnelServiceImpl implements PersonnelService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Autowired
    private DictDetailService dictDetailService;

    @Autowired
    private WeWeddingStyleService weWeddingStyleService;

    @Override
    public List<PageData> queryPersonnelList(PageData pageData) throws Exception {
        List<PageData> personnelList = (List<PageData>) dao.findForList("PersonnelMapper.queryPersonnelList", pageData);
        if (!CollectionUtils.isEmpty(personnelList)) {

            Map<String, String> typeMap = dictDetailService.queryDictDetailByName("personnel_type", "map");

            for (PageData personnel : personnelList) {
                int sex = (int) personnel.get("sex");
                String type = personnel.get("type") + "";
                personnel.put("type", type);
                if (sex == 0) {
                    personnel.put("sexStr", "女");
                } else {
                    personnel.put("sexStr", "男");
                }

                String typeStr = typeMap.get(type);
                if (StringUtils.isNotBlank(typeStr)) {
                    personnel.put("typeStr", typeStr);
                }
            }
        }
        return personnelList;
    }

    @Override
    public int create(PageData pageData) throws Exception {
        String uid = UuidUtil.get32UUID();
        pageData.put("id", uid);
        pageData.put("isDelete", 0);
        pageData.put("createTime", DateUtils.getTime());
        pageData.put("updateTime", DateUtils.getTime());

        String imageUrl = pageData.getString("imageUrl");
        String videoUrl = pageData.getString("videoUrl");
        if (StringUtils.isNotBlank(imageUrl)) {
            PageData pd = new PageData();
            pd.put("id", UuidUtil.get32UUID());
            pd.put("relationId", uid);
            pd.put("type", 0);//'类型：0：图片；1：视频',
            pd.put("url", imageUrl);
            pd.put("sort", 0);
            pd.put("isDelete", 0);
            pd.put("createTime", DateUtils.getTime());
            pd.put("updateTime", DateUtils.getTime());
            weWeddingStyleService.insterImg(pd);
        }
        if (StringUtils.isNotBlank(videoUrl)) {
            PageData pd = new PageData();
            pd.put("id", UuidUtil.get32UUID());
            pd.put("relationId", uid);
            pd.put("type", 1);//'类型：0：图片；1：视频',
            pd.put("url", videoUrl);
            pd.put("sort", 0);
            pd.put("isDelete", 0);
            pd.put("createTime", DateUtils.getTime());
            pd.put("updateTime", DateUtils.getTime());
            weWeddingStyleService.insterImg(pd);
        }

        return (int) dao.save("PersonnelMapper.insertPersonnel", pageData);
    }

    @Override
    public void deleteAll(String[] ids) throws Exception {
        try {
            //软删除实际是更新
            List<String> idList = new ArrayList<>(Arrays.asList(ids));
            dao.delete("PersonnelMapper.deleteAll", idList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(PageData pageData) throws Exception {

        String imageId = pageData.getString("imageId");
        if (StringUtils.isNotBlank(imageId)) {
            PageData imgInfo = new PageData();
            imgInfo.put("id", imageId);
            imgInfo.put("imageUrl", pageData.getString("imageUrl"));
            dao.save("PersonnelMapper.updateWeVideoPicture", imgInfo);
        } else {
            PageData pd = new PageData();
            pd.put("id", UuidUtil.get32UUID());
            pd.put("relationId", pageData.getString("id"));
            pd.put("type", 0);//'类型：0：图片；1：视频',
            pd.put("url", pageData.getString("imageUrl"));
            pd.put("sort", 0);
            pd.put("isDelete", 0);
            pd.put("createTime", DateUtils.getTime());
            pd.put("updateTime", DateUtils.getTime());
            weWeddingStyleService.insterImg(pd);
        }

        String videoId = pageData.getString("videoId");
        if (StringUtils.isNotBlank(videoId)) {
            PageData videoInfo = new PageData();
            videoInfo.put("id", pageData.getString("videoId"));
            videoInfo.put("imageUrl", pageData.getString("videoUrl"));
            dao.save("PersonnelMapper.updateWeVideoPicture", videoInfo);
        } else {
            PageData pd = new PageData();
            pd.put("id", UuidUtil.get32UUID());
            pd.put("relationId", pageData.getString("id"));
            pd.put("type", 1);//'类型：0：图片；1：视频',
            pd.put("url", pageData.getString("videoUrl"));
            pd.put("sort", 0);
            pd.put("isDelete", 0);
            pd.put("createTime", DateUtils.getTime());
            pd.put("updateTime", DateUtils.getTime());
            weWeddingStyleService.insterImg(pd);
        }

        dao.update("PersonnelMapper.updatePersonnelInfo", pageData);
    }

    @Override
    public void updateIsOpenState(PageData pageData) throws Exception {
        dao.update("PersonnelMapper.updateWeddingTypeInfo", pageData);
    }
}
